// ============================== 导入依赖 ============================== //

const SRC = 'exceljs@4/dist/exceljs.min.js'

// ============================== 导出文档工具 ============================== //

export default {
  /**
   * 文档导出
   *
   * @param {String} filename 文档名称 (不带 .xlsx 后缀，为空则根据当前时间自动生成)
   * @param {Object} sheet1 工作簿一 (见底部示例和说明)
   * @param {Object} sheet2 工作簿二
   * @param {Object} sheetn 工作簿几
   */
  excelExport(filename, ...sheetList) {
    // 打开遮罩
    this.loading()
    // 延时关闭
    const closeLoading = () => setTimeout(() => this.unloading(), 400)

    // 初始化插件
    this.loadJS(this.cdn(SRC), 'ExcelJS')
      // 成功，则导出文件
      .then(() => {
        exportExcel(this, filename, sheetList)
        this.MSG.success('导出：成功')
        closeLoading()
      })

      // 失败，则提示错误
      .catch((e) => {
        this.logError('导出 Excel', { filename, sheetList }, e)
        this.MSG.error(e.message)
        closeLoading()
      })
  },
}

// ============================== 内部方法 ============================== //

const VIEWS = [{ state: 'frozen', xSplit: 1, ySplit: 1 }]

const line = { style: 'thin', color: { argb: 'FF888888' } }
const border = { top: line, right: line, bottom: line, left: line }
const alignment = { wrapText: true, vertical: 'middle', horizontal: 'center' }
const STYLE_ALL = { font: { size: 12 }, alignment, border }
const STYLE_TITLE = { font: { size: 14, bold: true }, alignment, border }

const exportExcel = (CORE, filename, sheetList) => {
  const { ExcelJS } = window
  const workbook = new ExcelJS.Workbook()

  sheetList.forEach(({ sheetName, titleList = [], widthList = [], arrayList = [[]], styleList = [], mergeList = [[]] }) => {
    const noTitle = CORE.isEmpty(titleList)

    // 创建工作簿
    const sheet = workbook.addWorksheet(sheetName, { views: VIEWS })

    // 添加数据
    const transWidth = (i) => (widthList[i] || 160) / 20 * 3 | 0
    if (!noTitle) sheet.columns = titleList.map((header, i) => ({ header, key: i, width: transWidth(i) }))
    sheet.addRows(arrayList)

    // 设置样式
    titleList.forEach((_, i) => Object.assign(sheet.getColumn(i + 1), CORE.merge({}, STYLE_ALL, styleList[i])))
    if (!noTitle) titleList.map((_, i) => Object.assign(sheet.getRow(1).getCell(i + 1), STYLE_TITLE))
  })

  workbook.xlsx.writeBuffer().then((binary) => CORE.fileSave(binary, filename))
}

// const transMerge = (x, noTitle) => {
//   const add = noTitle ? 0 : 1
//   return {
//     s: { r: x[0] + add, c: x[1] },
//     e: { r: x[2] + add, c: x[3] },
//   }
// }

// ============================== 示例配置 ============================== //

const sheetName = 'Sheet1'

const titleList = ['姓名', '年龄', '学历', '毕业时间']
const widthList = [100, 80, 100, 120]

const arrayList = [
  ['张三', 23, '本科', '2013 - 07'],
  ['张三', 23, '硕士', '2016 - 07'],
  ['李四', 24, '本科', '2014 - 07'],
]
const megerList = [
  [0, 0, 1, 0], // A1 (0,0) 与 A2 (1,0) 合并
  [0, 1, 1, 1], // B1 (0,1) 与 B2 (1,1) 合并
]

export const sheetDemo = {
  sheetName, // 工作簿名称 (可选，为空则使用 Sheet1/Sheet2/.../sheetN)

  titleList, // 标题列表 (可选，为空则第一行直接放数据)
  widthList, // 宽度列表 (可选，有几个则设置几个的宽度，单位磅)

  arrayList, // 数组列表 (必传)
  megerList, // 合并列表 (可选，行列都从 0 开始，只跟 arrayList 二维数据有关，有无标题列都一样)
}
